package com.next

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
@Transactional(readOnly = true)
class ProjectTeamController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService

    def index(Integer max)
    {

        def username = springSecurityService?.getPrincipal()?.username
        def projectTeamList = ProjectTeam.findAll("from ProjectTeam where user.username = '${username}'", [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = ProjectTeam.executeQuery("select count(id) from ProjectTeam where user.username = '${username}'")

        respond projectTeamList, model: [projectTeamCount: count[0]]
    }

    def show(ProjectTeam projectTeam)
    {
        respond projectTeam
    }

    def create()
    {
        def project = Project.findById(params['project'])
        def user = User.findByUsername(springSecurityService?.getPrincipal()?.username)
        def userList = User.list()
        def projectTeams = ProjectTeam.findAllByProject(project)
        projectTeams?.each {
            userList -= it?.user
        }
        respond new ProjectTeam(params), model: [userList: userList]
    }

    @Transactional
    def save(ProjectTeam projectTeam)
    {
        def userList = User.list()
        def projectTeams = ProjectTeam.findAllByProject(projectTeam?.project)
        projectTeams?.each {
            userList -= it?.user
        }

        if (projectTeam == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (projectTeam.hasErrors())
        {
            //COMMENT THE INFO THEN YOU WILL SEE MIRACLES
            log.info "userList:" + userList

            transactionStatus.setRollbackOnly()
            respond projectTeam.errors, model: [userList: userList], view: 'create'
            return
        }

        projectTeam.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'projectTeam.label', default: 'ProjectTeam'), projectTeam.id])
                // redirect projectTeam
                redirect(controller: "project", action: "show", params: [id: projectTeam?.project?.id])
            }
            '*' { respond projectTeam, [status: CREATED] }
        }
    }

    def edit(ProjectTeam projectTeam)
    {
        respond projectTeam
    }

    @Transactional
    def update(ProjectTeam projectTeam)
    {
        if (projectTeam == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (projectTeam.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond projectTeam.errors, view: 'edit'
            return
        }

        projectTeam.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'projectTeam.label', default: 'ProjectTeam'), projectTeam.id])
                // redirect projectTeam
                redirect(controller: "project", action: "show", params: [id: projectTeam?.project?.id])
            }
            '*' { respond projectTeam, [status: OK] }
        }
    }

    @Transactional
    def delete(ProjectTeam projectTeam)
    {

        if (projectTeam == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        projectTeam.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'projectTeam.label', default: 'ProjectTeam'), projectTeam.id])
                // redirect action:"index", method:"GET"
                redirect(controller: "project", action: "show", params: [id: projectTeam?.project?.id])
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'projectTeam.label', default: 'ProjectTeam'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    def searchProjectTeam()
    {
        def username = springSecurityService?.getPrincipal()?.username
        def name = params["name"]
        String sql = "from ProjecttTeam as u where 1=1 and u.user.username = '${username}'"
        if (name)
        {
            sql += " and u.project.name like '%${name}%'"
        }

        String countSql = "select count(u.id) " + sql

        sql += ' order by createdDate desc'

        def projectTeamList = ProjectTeam.findAll(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = ProjectTeam.executeQuery(countSql)

        respond projectTeamList, model: [projectTeamCount: count[0], params: params], view: 'index'
    }
}
